﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace YXShop.Web.membercenter.accountrun
{
    public partial class integral_list : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["MemberID"] == null)
                {
                    ChangeHope.WebPage.Script.AlertAndGoBack("温馨提示：登录超时，请重新登录！");
                }
                string type = ChangeHope.Common.StringHelper.InputTexts(ChangeHope.WebPage.PageRequest.GetQueryString("type"));
                if (type != "" && type != null)
                {
                    Cache["type"] = type;
                }
                else
                {
                    Cache["type"] = "3";
                }
                if (Cache["type"].ToString() == "3")//登录积分列表
                {
                    this.lblList.Text = GetList();
                }
                
                else if (Cache["type"].ToString() == "1")//购物积分列表
                {
                    this.lblList.Text = GetOrderList();
                }

                GetSumInfo();
            }
        }

        protected string GetMoreInfoList(int type)
        {
            string url = YXShop.TemplateAction.Common.UrlStr("Integral_list", "type="+type.ToString(), "membercenter/accountrun/");
            string getDetail = string.Format("     <a href='{0}' title='点击查看详情'>查看详情</a>", url);
            return getDetail;
        }

        protected void GetSumInfo()
        {
            YXShop.BLL.Member.Integral bll = new YXShop.BLL.Member.Integral();
            this.lblInteCount.Text = bll.GetInteSumByUid(Convert.ToInt32(Session["MemberID"].ToString())).ToString();
            //登陆赠送积分
            this.lblLoginInte.Text = bll.GetInteSumByUid(Convert.ToInt32(Session["MemberID"].ToString()), 3).ToString() + GetMoreInfoList(3);
            //订单
            this.lblOrderInte.Text = bll.GetInteSumByUid(Convert.ToInt32(Session["MemberID"].ToString()), 1).ToString() + GetMoreInfoList(1);
        }

        /// <summary>
        /// 积分来源(1商品订单,2评论,3登陆)
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        protected string GetList()
        {
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            YXShop.BLL.Member.Integral bll = new YXShop.BLL.Member.Integral();
            ChangeHope.DataBase.DataByPage dataPage = bll.GetList(" 1=1 and userId=" + Session["MemberID"].ToString() + " and integralClass=3");
            //第一步先添加表头
            table.AddHeadCol("", "来源内容");
            table.AddHeadCol("", "获得积分");
            table.AddHeadCol("", "获得时间");
            table.AddHeadCol("", "处理者");
            table.AddHeadCol("", "备注");
            table.AddRow();
            //添加表的内容
            if (dataPage.DataReader != null)
            {
                while (dataPage.DataReader.Read())
                {
                    string remark = ChangeHope.Common.StringHelper.SubStringAndAppend(dataPage.DataReader["remark"].ToString(), 100, "...");
                    table.AddCol(dataPage.DataReader["origin"].ToString());
                    string integral = dataPage.DataReader["integral"].ToString();
                    if (dataPage.DataReader["integralStatus"].ToString() == "1")
                        integral = "-" + integral;
                    table.AddCol(integral);
                    table.AddCol(dataPage.DataReader["gainDate"].ToString());
                    table.AddCol("系统");
                    table.AddCol(remark);
                    table.AddRow();
                }
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            return view;
        }

        /// <summary>
        /// 积分来源(1商品订单,2评论,3登陆)
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        protected string GetOrderList()
        {
            ChangeHope.WebPage.Table table = new ChangeHope.WebPage.Table();
            YXShop.BLL.Member.Integral bll = new YXShop.BLL.Member.Integral();
            ChangeHope.DataBase.DataByPage dataPage = bll.GetList(" 1=1 and userId=" + Session["MemberID"].ToString() + " and integralClass=1");
            //第一步先添加表头
            table.AddHeadCol("", "订单ID");
            table.AddHeadCol("", "来源内容");
            table.AddHeadCol("", "获得积分");
            table.AddHeadCol("", "获得时间");
            table.AddHeadCol("", "处理者");
            table.AddHeadCol("", "备注");
            table.AddRow();
            //添加表的内容
            if (dataPage.DataReader != null)
            {
                while (dataPage.DataReader.Read())
                {
                    table.AddCol(dataPage.DataReader["orderid"].ToString());
                    string remark = ChangeHope.Common.StringHelper.SubStringAndAppend(dataPage.DataReader["remark"].ToString(), 100, "...");
                    table.AddCol(dataPage.DataReader["origin"].ToString());
                    string integral = dataPage.DataReader["integral"].ToString();
                    if (dataPage.DataReader["integralStatus"].ToString() == "1")
                        integral = "-" + integral;
                    table.AddCol(integral);
                    table.AddCol(dataPage.DataReader["gainDate"].ToString());
                    table.AddCol(dataPage.DataReader["noteName"].ToString());
                    table.AddCol(remark);
                    table.AddRow();
                }
            }
            string view = table.GetTable() + dataPage.PageToolBar;
            dataPage.Dispose();
            dataPage = null;
            return view;
        }
    }
}
